package com.example.myapplication.ui.news.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.myapplication.R;
import com.example.myapplication.app.AppConstant;
import com.example.myapplication.base.BaseMvpFragment;
import com.example.myapplication.model.bean.VideoData;
import com.example.myapplication.ui.news.NewsPresenter.VideoListPresenter;
import com.example.myapplication.ui.news.NewsPresenter.VideosListContract;
import com.example.myapplication.ui.news.adapter.VideoAdapter;
import com.xiao.nicevideoplayer.NiceVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class VideosFragment extends BaseMvpFragment<VideoListPresenter> implements VideosListContract.View{

    @BindView(R.id.ic_view)
    RecyclerView ic_view;
    private String mVideoType;
    VideoAdapter adapter;
    private int mStartPage=0;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_videos;
    }
    @Override
    protected VideoListPresenter initPresenter() {
        return new VideoListPresenter(getActivity());
    }


    @Override
    protected void initView() {
        if (getArguments() != null) {
            mVideoType = getArguments().getString(AppConstant.VIDEO_TYPE);
        }
        ic_view.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new VideoAdapter(R.layout.item_video_list,new ArrayList<>());
        ic_view.setAdapter(adapter);
        //视频监听
        ic_view.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }
            @Override
            public void onChildViewDetachedFromWindow(View view) {
                NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
                if (niceVideoPlayer != null) {
                    niceVideoPlayer.release();
                }
//                if (JCVideoPlayerManager.listener() != null) {
//                    JCVideoPlayer videoPlayer = (JCVideoPlayer) JCVideoPlayerManager.listener();
//                    if (((ViewGroup) view).indexOfChild(videoPlayer) != -1 && videoPlayer.currentState == JCVideoPlayer.CURRENT_STATE_PLAYING) {
//                        JCVideoPlayer.releaseAllVideos();
//                    }
//                }
            }
        });
    }
    @Override
    protected void initEventAndData() {
        mPresenter.requestDataRefresh(mVideoType,mStartPage);
    }
    @Override
    public void showError(String msg) {

    }

    @Override
    public void SuccessRefresh(List<VideoData> newsSummaries) {
        adapter.addData(newsSummaries);
    }


}
